/**
 * 项目名称（软件名称）：忠旺机械物资管理系统(mms)
 * 研发设计单位：忠旺机械设备制造有限公司机械设计院智能控制设计科设计与研发。
 * 版权所有：忠旺机械设备制造有限公司
 * 注：本项目的一切技术专利，使用规范归解释权由忠旺机械设备制造有限公司所有。
 * 软件版本：Ver 1.0.0
 * 文件路径：com.zhongwang.mms.module.purchase.controller
 * 创建时间：2020年04月11日 16时42分
 **/
package com.zhongwang.mms.module.purchase.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.purchase.model.*;
import com.zhongwang.mms.module.purchase.service.PurInvProService;
import com.zhongwang.mms.util.DateUtil;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName： PurChangeController
 * @Description： 库存划项目控制层
 * @Author： 徐一贺
 * @Date： 2020/4/11 16:42
 * @Version： V1.0
 **/
@Log4j2
@Controller
@RequestMapping("/purchase/invPro")
public class PurInvProController extends BaseController {
    @Autowired
    private PurInvProService purInvProService;


    @GetMapping("/index")
    /**
     *@FunctionName: index
     *@Description: 跳转到库存划项目申请列表页面
     *@Author: 徐一贺
     *@CreateDate: 2020/4/18 11:20
     *@Phone: 18241927380
     *@Return: java.lang.String 页面路径
     *@Version: 1.0.0
     */
    public String index() {
        return "/purchase/purInvManage/invPro/page";
    }

    /**
     * @FunctionName: createPage
     * @Description: 跳转到创建库存划项目申请页面
     * @Author: 徐一贺
     * @CreateDate: 2020/4/18 11:20
     * @Phone: 18241927380
     * @Return: java.lang.String 页面路径
     * @Version: 1.0.0
     */
    @GetMapping("/createPage")
    public String createPage(Model model) {
        model.addAttribute("ceoAppPerson", ShiroUtils.getUser().getName());
        model.addAttribute("ceoAppPersonId", ShiroUtils.getUser().getId());
        model.addAttribute("ceoAppDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString());
        return "/purchase/purInvManage/invPro/create/page";
    }


    /**
     * @FunctionName: execPage
     * @Description: 跳转到库存划项目申请执行页面
     * @Author: 徐一贺
     * @CreateDate: 2020/4/21 19:37
     * @Phone: 18241927380
     * @Return: java.lang.String 页面路径
     * @Version: 1.0.0
     */
    @GetMapping("/execPage")
    public String execPage() {
        return "/purchase/purInvManage/invPro/exec/page";
    }

    /**
     * @FunctionName: execPage
     * @Description: 跳转到库存划项目申请执行页面
     * @Author: 徐一贺
     * @CreateDate: 2020/4/21 19:37
     * @Phone: 18241927380
     * @Return: java.lang.String 页面路径
     * @Version: 1.0.0
     */
    @GetMapping("/execPage/{totalPrice}//{year}/{month}")
    public String execPageJump(Model model, @PathVariable("totalPrice") String totalPrice, @PathVariable("year") Integer year, @PathVariable("month") Integer month) {
        model.addAttribute("mode", "index_exec");
        model.addAttribute("totalPrice", totalPrice);
        String start = DateUtil.getFirstDayOfMonth(month);
        start = start.replace(start.substring(0, start.indexOf("-")), year.toString());
        model.addAttribute("start", start.substring(0, 10));
        String end = DateUtil.getLastDayOfMonth(month);
        end = end.replace(end.substring(0, end.indexOf("-")), year.toString());
        model.addAttribute("end", end.substring(0, 10));
        return "/purchase/purInvManage/invPro/exec/page";
    }


    /**
     * @FunctionName: importPage
     * @Description: 库存划项目标记导入页
     * @Author: 徐一贺
     * @CreateDate: 2020年6月17日12:59:06
     * @Phone: 18241927380
     * @Param:
     * @Return:
     * @Version:
     */
    @GetMapping("/importPage")
    public String importPage(Model model) {
        model.addAttribute("userId", ShiroUtils.getUserId());
        return "/purchase/purInvManage/invPro/import/page";
    }

    /**
     * @FunctionName: importPage
     * @Description: 库存划项目标记模板导出页
     * @Author: 徐一贺
     * @CreateDate: 2020年6月17日12:59:02
     * @Phone: 18241927380
     * @Param:
     * @Return:
     * @Version:
     */
    @GetMapping("/exportPage")
    public String exportPage(Model model) {
        model.addAttribute("mode", "invPro");
        return "/purchase/purInvManage/change/export/page";
    }


    @PostMapping("/inventoryList")
    @ResponseBody
    /**
     *@FunctionName: inventoryList
     *@Description: 获取库存在库库存列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/23 9:15
     *@Phone: 18241927380
     *@Param: [param] 查询参数
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult inventoryList(PInventoryModel param) {
        startPage();
        List<PInventoryModel> inventoryList = purInvProService.findInventoryList(param);
        return jsonResult(inventoryList);
    }

    @PostMapping("/findProCodeList")
    @ResponseBody
    /**
     *@FunctionName: findProCodeList
     *@Description: 根据项目号模糊查询获取看板系统项目信息与本地系统项目状态列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/18 12:50
     *@Phone: 18241927380
     *@Param: [param] 参数
     *@Return: com.zhongwang.mms.common.JsonResult 返回查询结果
     *@Version: 1.0.0
     */
    public JsonResult findProCodeList(VWProject param) {
        List<VWProject> vwProjectList = purInvProService.findVWProjectByLikeCode(param);
        return JsonResult.success("", vwProjectList);
    }

    @PostMapping("/list")
    @ResponseBody
    @RequiresPermissions({"purinv:purwarpro:view"})
    /**
     *@FunctionName: list
     *@Description: 获取库存划项目申请列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/15 19:39
     *@Phone: 18241927380
     *@Param: [queryModel] 检索参数
     *@Return: [List<ChangeModel>] 库存划项目申请列表
     *@Version: 1.0.0
     */
    public JsonResult list(PInventoryProjectMarkQueryModel queryModel) {
        startPage();
        return jsonResult(purInvProService.findPInventoryProjectMarkListByQuery(queryModel));
    }

    @PostMapping("/execList")
    @ResponseBody
    @RequiresPermissions({"purinv:purwaprorexec:view"})
    /**
     *@FunctionName: execList
     *@Description: 获取库存划项目执行列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/15 19:39
     *@Phone: 18241927380
     *@Param: [queryModel] 检索参数
     *@Return: [List<ChangeModel>] 库存划项目执行列表
     *@Version: 1.0.0
     */
    public JsonResult execList(PInventoryProjectMarkQueryModel queryModel) {
        startPage();
        return jsonResult(purInvProService.findPInventoryProjectMarkExecListByQuery(queryModel));
    }


    @PostMapping("/deptList")
    @ResponseBody
    /**
     *@FunctionName: deptList
     *@Description: 获取单位列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/21 10:12
     *@Phone: 18241927380
     *@Param: []
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult deptList() {
        return JsonResult.success("", purInvProService.findDeptList());
    }


    @PostMapping("codeExist")
    @ResponseBody
    /**
     *@FunctionName: codeExist
     *@Description: bootstrapValidate验证：验证项目号是否存在
     *@JavaScriptDemo： notEmpty: {message: '请输入需求项目编号'},
     *                         threshold: 6, //有6字符以上才发送ajax请求，（input中输入一个字符，插件会向服务器发送一次，设置限制，6字符以上才开始）
     *                         remote: {//ajax验证。server result:{"valid",true or false} 向服务发送当前input name值，获得一个json数据。例表示正确：{"valid",true}
     *                             url: 'purchase/change/codeExist',//验证地址
     *                             message: '用户已存在',//提示消息
     *                             delay: 100,//每输入一个字符，就发ajax请求，服务器压力还是太大，设置2秒发送一次ajax（默认输入一个字符，提交一次，服务器压力太大）
     *                             type: 'POST'//请求方式
     *                             /**自定义提交数据，默认值提交当前input value
     *                              *  data: function(validator) {
     *                                return {
     *                                    password: $('[name="passwordNameAttributeInYourForm"]').val(),
     *                                    whatever: $('[name="whateverNameAttributeInYourForm"]').val()
     *                                };
     *                             }
     *
     *
     *@Author: 徐一贺
     *@CreateDate: 2020/4/18 13:24
     *@Phone: 18241927380
     *@Param: [param] 项目号
     *@Return: java.lang.Boolean
     *@Version: 1.0.0
     */
    public String codeExist(VWProject param) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", purInvProService.codeExist(param));
        String resultString = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultString;
    }


    @PostMapping("proInfoExist")
    @ResponseBody
    /**
     *@FunctionName: 根据项目号查询本系统项目是否存在
     *@Description: 请填写方法描述
     *@Author: 徐一贺
     *@CreateDate: 2020/4/20 13:31
     *@Phone: 18241927380
     *@Param: [param] 项目号
     *@Return: [String] 返回结果
     *@Version: 1.0.0
     */
    public String proInfoExist(VWProject param) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", purInvProService.proInfoExist(param));
        String resultString = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultString;
    }

    @PostMapping("/save")
    @ResponseBody
    @RequiresPermissions({"purinv:purwarpro:edit"})
    /**
     *@FunctionName: save
     *@Description: 保存库存划项目标记
     *@Author: 徐一贺
     *@CreateDate: 2020/4/20 11:17
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult save(PInventoryProjectMarkModel param) {
        return purInvProService.saveInvPro(param);
    }

    @PostMapping("/submitAppy")
    @ResponseBody
    @RequiresPermissions({"purinv:purwarpro:edit"})
    /**
     *@FunctionName: submitAppy
     *@Description: 提交
     *@Author: 徐一贺
     *@CreateDate: 2020/4/21 12:39
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult submitAppy(PInventoryProjectMarkModel param) {
        if (purInvProService.submitAppy(param)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @PostMapping("/execApply")
    @ResponseBody
    @RequiresPermissions({"purinv:purwarproexec:edit"})
    /**
     *@FunctionName: execAppy
     *@Description: 执行
     *@Author: 徐一贺
     *@CreateDate: 2020/4/21 12:39
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult execApply(PInventoryProjectMarkModel param) {
        JsonResult JsonResult = new JsonResult();
        try {
            JsonResult = purInvProService.execApply(param);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            JsonResult.setType(com.zhongwang.mms.common.JsonResult.ResultType.ERROR);
            JsonResult.setMsg("执行失败！");
        }
        return JsonResult;
    }

    @PostMapping("/removeAppy")
    @ResponseBody
    @RequiresPermissions({"purinv:purwarpro:edit"})
    /**
     *@FunctionName: removeAppy
     *@Description: 删除
     *@Author: 徐一贺
     *@CreateDate: 2020/4/21 12:39
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult removeAppy(PInventoryProjectMarkModel param) {
        if (purInvProService.removeAppy(param)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @PostMapping("/invalid")
    @ResponseBody
    @RequiresPermissions({"purinv:purwarproexec:edit"})
    /**
     *@FunctionName: invalid
     *@Description: 驳回
     *@Author: 徐一贺
     *@CreateDate: 2020/4/21 12:39
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult invalid(PInventoryProjectMarkModel param) {
        if (purInvProService.invalid(param)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @PostMapping("/revokeApply")
    @ResponseBody
    @RequiresPermissions({"purinv:purwarproexec:edit"})
    /**
     *@FunctionName: revokeAppy
     *@Description: 撤销
     *@Author: 徐一贺
     *@CreateDate: 2020/4/22 9:16
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult revokeApply(PInventoryProjectMarkModel param) {
        if (purInvProService.revokeAppy(param)) {
            return JsonResult.success();
        }
        return JsonResult.fail("因库存划项目需求的库存划项目库存信息因被标记或库存不足导致无法撤销！");
    }

    @PostMapping("/getInfo")
    @ResponseBody
    /**
     *@FunctionName: getInfo
     *@Description: 根据主键获取库存划项目实体
     *@Author: 徐一贺
     *@CreateDate: 2020/5/14 13:24
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult getInfo(PInventoryProjectMarkModel param) {
        return JsonResult.success("获取库存划项目信息成功！", purInvProService.getInfo(param));
    }

    @PostMapping("/saveEdit")
    @ResponseBody
    @RequiresPermissions({"purinv:purwarpro:edit"})
    /**
     *@FunctionName: saveEdit
     *@Description: 保存修改
     *@Author: 徐一贺
     *@CreateDate: 2020/5/14 13:48
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult saveEdit(PInventoryProjectMarkModel param) {
        return purInvProService.saveEdit(param);
    }

    @PostMapping("/importExcel")
    @ResponseBody
    /**
     *@FunctionName: importExcel
     *@Description: 导入批量库存划项目标识Excel
     *@DesignIdea: 请填写设计思想
     *@Author: 徐一贺
     *@CreateDate: 2020/6/15 14:49
     *@Phone: 18241927380
     *@Param: [file]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Throw:
     *@Version: v 1.0.0
     */
    public JsonResult importExcel(MultipartFile file) {
        return purInvProService.importExcel(file);
    }

    @PostMapping("/getPInventory")
    @ResponseBody
    /**
     *@FunctionName: getPInventory
     *@Description: 根据主键过去库存实体
     *@DesignIdea: 根据主键过去库存实体
     *@Author: 徐一贺
     *@CreateDate: 2020/6/22 14:09
     *@Phone: 18241927380
     *@Param:
     *@Return:
     *@Throw:
     *@Version: v 1.0.0
     */
    public JsonResult getPInventory(PInventoryModel param) {
        return JsonResult.success("", purInvProService.getPInventory(param));
    }


    /**
     * 列表 导出
     *
     * @param param 查询条件
     * @return list
     */
    @GetMapping("/exportExcel")
    @ResponseBody
    public void exportExcel(HttpServletResponse response, PInventoryProjectMarkQueryModel param) {
        purInvProService.exportExcel(response, param);
    }
}